/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  自定义光标方法集合
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-07-12
 * 包括以下静态方法
 * .cCustCursor.getCursorPosition(); // 获得光标位置
 * .cCustCursor.setCursorPosition(); // 设置光标位置
 * .cCustCursor.add();			   // 添加内容
 * .cCustCursor.del();			   // 删除内容
 * .cCustCursor.sel();               // 选中指定区域的内容
 */
LJS.define('mod.cCustCursor',function(ljs){

     var cCustCursor = {};
    /**
     * 获得光标位置
     * @param {Object} el 光标所在的dom
     * @return {Object} {
     * 	start:'',
     * 	end:''
     * }
     * IE下只能获得开始位置,结束位置与开始位置相同
     */
    cCustCursor.getCursorPosition = function(el){
        // IE和web-kit缺省光标位置在0;
        // FF缺省光标位置在el.value.length;
        // IE在选取内容时,光标记录的是起始位置;
        // FF和web-kit在选取内容时,光标记录的是靠左的位置;
        var _start = 0;
        var _end = el.value.length;
        var _ds = document.selection || '';
        if (document.selection) {
            /*
            var _range = _ds.createRange();
            var _storedRange = _range.duplicate();
            _storedRange.moveToElementText(el);
            _storedRange.setEndPoint("EndToEnd", _range);
            el.selectionStart = _storedRange.text.length - _range.text.length;
            el.selectionEnd = el.selectionStart + _range.text.length;
            _start = el.selectionStart;
            _end = el.selectionEnd;
            */
            _start = el.value.length;
            _end = el.value.length;
        } else if (el.selectionStart || el.selectionStart == "0") {
            _start = el.selectionStart;
            _end = el.selectionEnd;
        }
        return {
            'start': _start,
            'end': _end
        };
    };
    /**
     * 设置光标位置
     * @param {Object} el 光标所在的dom
     * @param {Number} pos
     */
    cCustCursor.setCursorPosition = function(el, pos){
        var that = this;
        that.sel(el, pos, pos);
    };
    /**
     * 添加内容
     * @param {Object} el 光标所在的dom
     * @param {String} content 添加内容
     * @param {String || Number} where 在哪嵌入
     * allBefore 最前 || before 光标前 || after 光标后 || allBehind 最后 || number 具体位置
     * @param {Object} range IE的选块
     */
    cCustCursor.add = function(el, content, where, range){
        var that = this;
        setTimeout(function(){
            el.focus();
            setTimeout(function(){ //ie6 focus bug fix
                var _value = el.value;
                if($.browser.msie && el.tagName=="INPUT"){
                    el.blur();
                }
                if(_value==''){
                    el.value = '';
                }
                var _start = that.getCursorPosition(el).start;
                var _end = that.getCursorPosition(el).end;
                var _stopPos = 0;
                var _preValue = el.value.substring(0, _start);
                var _nextValue = el.value.substring(_end, el.value.length);
                switch (where) {
                    case 'allBefore':
                        _preValue = '';
                        _nextValue = el.value;
                        _stopPos = 0;
                        break;
                    case 'before':
                        if (range) {
                            // IE如果有range选块,把range选块内容置为空
                            range.text = '';
                            _preValue = el.value.substring(0, that.getCursorPosition(el).start);
                            _nextValue = el.value.substring(that.getCursorPosition(el).start, el.value.length);
                        }
                        _stopPos = _preValue.length + content.length;
                        break;
                    case 'after':
                        if (range) {
                            // IE如果有range选块,把range选块内容置为空
                            range.text = '';
                            _preValue = el.value.substring(0, that.getCursorPosition(el).start);
                            _nextValue = el.value.substring(that.getCursorPosition(el).start, el.value.length);
                        }
                        _stopPos = _preValue.length;
                        break;
                    case 'allBehind':
                        _preValue = el.value;
                        _nextValue = '';
                        _stopPos = el.value.length + content.length;
                        break;
                    default:
                        if (where || where === 0) {
                            that.setCursorPosition(el, where);
                            _start = that.getCursorPosition(el).start;
                            _end = that.getCursorPosition(el).end;
                            _preValue = el.value.substring(0, _start);
                            _nextValue = el.value.substring(_start, el.value.length);
                            _stopPos = _preValue.length + content.length;
                        }
                }
                el.value = _preValue + content + _nextValue;
                that.setCursorPosition(el, _stopPos);
                el.focus();
            },10);
        },10)
    };
    /**
     * 删除光标前面或者后面的length个字符(一个汉字算1字符)
     * @param {Object} el
     * @param {Number} length
     */
    cCustCursor.del = function(el, length){
        var that = this;
        var _pos = that.getCursorPosition(el).start;
        var _value = el.value;
        if (length > 0) {
            el.value = _pos >= length ? _value.slice(0, Math.abs(_pos - length)) + _value.slice(_pos) : _value.slice(_pos);
        } else {
            el.value = _value.slice(0, _pos) + _value.slice(_pos - length);
        }
        that.setCursorPosition(el, _pos - (length < 0 ? 0 : length));
    };
    /**
     * 选中从start到end位置的内容
     * @param {Object} el 光标所在的dom
     * @param {Number} start 起始位置
     * @param {Number} end 结束
     */
    cCustCursor.sel = function(el, start, end){
        if (document.selection) {
            var _range = el.createTextRange();
            _range.moveEnd('character', -el.value.length);
            _range.moveEnd('character', end);
            _range.moveStart('character', start);
            _range.select();
        } else {
            el.setSelectionRange(start, end);
            el.focus();
        }
    };
    return cCustCursor
});